#include "server.hpp"
#include <memory>

bool checkSafe(const std::string&comm)
{
    std::vector<std::string> unsafeComs{"kill","rm"};
    for(auto& str:unsafeComs)
    {
        if(comm.find(str)!=std::string::npos)
        {
            return false;
        }
    }
    return true;
}

std::string ExecCommand(const std::string resqust)
{
    if(!checkSafe(resqust))
    {
        return "不安全指令";
    }
    FILE* fp = popen(resqust.c_str(),"r");
    if(fp == nullptr)
    {
        return "无法执行命令";
    }
    std:: string ret;
    char buff[4096];
    while(fgets(buff,sizeof(buff),fp)!=nullptr)
    {
        ret+=buff;
    }
    fclose(fp);
    return ret;
}

int main()
{
    std::unique_ptr<yui::UdpServer> user(new yui::UdpServer(ExecCommand));
    //初始化服务器
    user->Initserver();
    //启动服务器
    user->StartServer();
    return 0;
}